<template>
  <div>
    <el-upload
      action="/admin/common/uploadImage"
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :http-request="uploadimg"
      :limit="3"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" src="imageUrl" alt="">
    </el-dialog>

  </div>
</template>

<script>
import { imgAPI } from '@/api/Bioinformatics'
export default {
  props: {
    value: {
      type: String,
      default: () => ''
    }
  },
  data() {
    return {
      imageUrl: '',
      dialogVisible: false
    }
  },
  watch: {
    value: {
      handler() {
        this.imageUrl = this.value
      },
      immediate: true
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      const isLt5M = file.size / 1024 / 1024 < 5
      if (!isLt5M) {
        this.$message.error('上传头像图片大小不能超过 5MB!')
      }
      return isLt5M
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePictureCardPreview(file) {
      console.log(file)
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    async uploadimg({ file }) {
      try {
        const res = await imgAPI(file)
        this.imageUrl = res
        this.$emit('input', this.imageUrl)
        console.log(this.imageUrl)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
